package com.qianfeng.wuyang.himalaya.fragment;


import android.app.Application;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MyApplication;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.NationalRecyclerAdapter;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.NationEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProvinceBaseFragment extends Fragment implements Callback<NationEntity>, SwipeRefreshLayout.OnRefreshListener {


    private NationalRecyclerAdapter provinceRecyclerAdapter;
    private int pageNum = 1;
    private SwipeRefreshLayout refresh_province;
    private int provinceCode;
    private RecyclerView recycler_ProvinceActivity;

    public ProvinceBaseFragment() {
        // Required empty public constructor
    }

    public static ProvinceBaseFragment newInstance(int provinceCode) {
        ProvinceBaseFragment fragment = new ProvinceBaseFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("provinceCode",provinceCode);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_province_base, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        TextView textView = (TextView) view.findViewById(R.id.textView);
//        String type =  getArguments().getString("type");
//        textView.setText(type);
//        http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&pageSize=20&provinceCode=110000&radioType=2
        provinceCode = getArguments().getInt("provinceCode");
        recycler_ProvinceActivity = (RecyclerView) view.findViewById(R.id.recycler_ProvinceActivity);
        MyApplication myApplication = (MyApplication) getActivity().getApplication();
        myApplication.getBcProvinceService().getData(1,20,provinceCode,2).enqueue(this);
        provinceRecyclerAdapter = new NationalRecyclerAdapter(getContext());
        recycler_ProvinceActivity.setAdapter(provinceRecyclerAdapter);
        recycler_ProvinceActivity.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //得到当前显示的最后一个item的view
                View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
                //得到lastChildView的bottom的坐标
                int lastChildBottom = lastChildView.getBottom();
                //得到Recyclerview的底部坐标减去底部padding值，也就是显示内容最底部的坐标
                int recyclerBottom =  recyclerView.getBottom()-recyclerView.getPaddingBottom();
                //通过这个lastChildView得到这个view当前的position值
                int lastPosition  = recyclerView.getLayoutManager().getPosition(lastChildView);
                if(lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ) {
                    pageNum=pageNum+1;
                    loadData(pageNum,provinceCode);
                }
            }
        });
        refresh_province = ((SwipeRefreshLayout) view.findViewById(R.id.refresh_province));
        //设置刷新的颜色渐变
        refresh_province.setColorSchemeResources(
                R.color.colorAccent,
                R.color.colorPrimary,
                R.color.tab_indicator,
                R.color.white
        );
        refresh_province.setOnRefreshListener(this);

        // 这句话是为了，第一次进入页面的时候显示加载进度条，调整进度条距离屏幕顶部的距离
        refresh_province.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
    }

    @Override
    public void onResponse(Call<NationEntity> call, Response<NationEntity> response) {
        if (response.body().getResult() != null) {
            provinceRecyclerAdapter.addAll(response.body().getResult());
        }
    }

    @Override
    public void onFailure(Call<NationEntity> call, Throwable t) {
            t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
    private void loadData(int pageNum,int provinceCode){
        MyApplication myApplication = (MyApplication) getActivity().getApplication();
        myApplication.getBcProvinceService().getData(pageNum,20,provinceCode,2).enqueue(this);
    }

    @Override
    public void onRefresh() {
        provinceRecyclerAdapter = new NationalRecyclerAdapter(getContext());
        recycler_ProvinceActivity.setAdapter(provinceRecyclerAdapter);
        loadData(1,provinceCode);
        refresh_province.setRefreshing(false);
    }
}
